﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Net;

namespace GamesEdit
{
    public partial class updata : Form
    {
        public updata(string 下载目录)
        {
            下载地址 = 下载目录+"Code.zip";
            InitializeComponent();
        }
        string 下载地址;
        
        private void updata_Load(object sender, EventArgs e)
        {
            //webBrowser1.Navigate("http://nds.512j.com/GameData/readme.txt");
            webBrowser1.Navigate("http://www.gamesedit.net/test/index.htm");
        }

        private void HtmlBtnClose_Click(object sender, EventArgs e)
        {
            //Application.Exit();


            this.Dispose(true);
            this.Close();
        }

        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            HtmlDocument htmlDoc = webBrowser1.Document;
            HtmlElement btnElement = htmlDoc.All["btnClose"];
            if (btnElement != null)
            {
                btnElement.Click += new HtmlElementEventHandler(HtmlBtnClose_Click);
            }
            btnElement = htmlDoc.All["btnDown"];
            if (btnElement != null)
            {
                btnElement.Click += new HtmlElementEventHandler(HtmlbtnDown_Click);
            }
        }

        private void HtmlbtnDown_Click(object sender, EventArgs e)
        {
            WebClient clientDownload = new WebClient();
            toolStripStatusLabel1.Text = "下载代码库中.....";
            clientDownload.DownloadFile(下载地址, Application.StartupPath + "\\Code.zip");//下载程序
            clientDownload.Dispose();
            string[] FileProperties = new string[2];
            FileProperties[0] = Application.StartupPath + "\\Code.zip";//待解压的文件 
            FileProperties[1] = Application.StartupPath +"\\";//解压后放置的目标目录 
            toolStripStatusLabel1.Text = "解压缩中.....";
            UnZip(FileProperties);
            toolStripStatusLabel1.Text = "更新完成.....";
            File.Delete(Application.StartupPath + "\\Code.zip");
        }

       /* private void button1_Click(object sender, EventArgs e)
        {
            //压缩Game.data.gz
               FileStream fs=  File.OpenRead(Application.StartupPath + "\\Game.data");
             byte[] data = new byte[fs.Length];
             fs.Read(data, 0, data.Length);
             fs.Close();

             FileStream fs1 = new FileStream("Game1.data.gz", FileMode.Create);
             GZipStream gz = new GZipStream(fs1, CompressionMode.Compress);
             gz.Write(data, 0, data.Length);
             gz.Close();
             fs1.Close();

            WebClient clientDownload = new WebClient();
            label1.Text="下载代码库中.....";
            clientDownload.DownloadFile(下载地址, Application.StartupPath + "\\Code.zip");//下载程序
            clientDownload.Dispose();
            string[] FileProperties = new string[2];
            FileProperties[0] = Application.StartupPath + "\\Code.zip";//待解压的文件 
            FileProperties[1] = Application.StartupPath + "\\";//解压后放置的目标目录 
            label1.Text = "解压缩中.....";
            UnZip(FileProperties);
            label1.Text = "完成.....";
            File.Delete(Application.StartupPath + "\\Code.zip");
        }*/


        public static  void UnZip(string[] args)
        {
            ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
   
            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {

                //string directoryName = Path.GetDirectoryName(args[1]);
                string directoryName = args[1];
                string fileName = Path.GetFileName(theEntry.Name);

                //生成解压目录 
                Directory.CreateDirectory(directoryName);

                if (fileName != String.Empty)
                {
                    //解压文件到指定的目录 
                    FileStream streamWriter = File.Create(args[1] + theEntry.Name);

                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }

                    streamWriter.Close();
                }
            }
            s.Close();
        }




    }
}
